{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Intermediate Neural Network in Keras"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook, we improve our [introductory shallow net](https://github.com/the-deep-learners/deep-learning-illustrated/blob/master/notebooks/shallow_net_in_keras.ipynb) by applying the theory we have covered since. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/the-deep-learners/deep-learning-illustrated/blob/master/notebooks/intermediate_net_in_keras.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Load dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from keras.optimizers import SGD"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(X_train, y_train), (X_valid, y_valid) = mnist.load_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Preprocess data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = X_train.reshape(60000, 784).astype('float32')\n",
    "X_valid = X_valid.reshape(10000, 784).astype('float32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train /= 255\n",
    "X_valid /= 255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_classes = 10\n",
    "y_train = keras.utils.to_categorical(y_train, n_classes)\n",
    "y_valid = keras.utils.to_categorical(y_valid, n_classes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Design neural network architecture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(Dense(64, activation='relu', input_shape=(784,)))\n",
    "model.add(Dense(64, activation='relu'))\n",
    "model.add(Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_1 (Dense)              (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 55,050\n",
      "Trainable params: 55,050\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Configure model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.1), metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Train!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 1s 15us/step - loss: 0.4744 - acc: 0.8637 - val_loss: 0.2686 - val_acc: 0.9234\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.2414 - acc: 0.9289 - val_loss: 0.2004 - val_acc: 0.9404\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.1871 - acc: 0.9452 - val_loss: 0.1578 - val_acc: 0.9521\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.1538 - acc: 0.9551 - val_loss: 0.1435 - val_acc: 0.9574\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.1311 - acc: 0.9616 - val_loss: 0.1258 - val_acc: 0.9616\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.1148 - acc: 0.9659 - val_loss: 0.1245 - val_acc: 0.9641\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.1017 - acc: 0.9700 - val_loss: 0.1066 - val_acc: 0.9683\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0914 - acc: 0.9728 - val_loss: 0.1029 - val_acc: 0.9672\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0821 - acc: 0.9760 - val_loss: 0.0942 - val_acc: 0.9709\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0738 - acc: 0.9785 - val_loss: 0.1035 - val_acc: 0.9691\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0672 - acc: 0.9796 - val_loss: 0.1000 - val_acc: 0.9710\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0617 - acc: 0.9820 - val_loss: 0.0913 - val_acc: 0.9735\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0570 - acc: 0.9835 - val_loss: 0.0817 - val_acc: 0.9754\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0526 - acc: 0.9844 - val_loss: 0.0917 - val_acc: 0.9729\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0477 - acc: 0.9861 - val_loss: 0.0822 - val_acc: 0.9752\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0450 - acc: 0.9868 - val_loss: 0.0845 - val_acc: 0.9752\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0413 - acc: 0.9878 - val_loss: 0.0842 - val_acc: 0.9741\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0384 - acc: 0.9887 - val_loss: 0.0833 - val_acc: 0.9752\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0356 - acc: 0.9903 - val_loss: 0.0803 - val_acc: 0.9760\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 1s 12us/step - loss: 0.0332 - acc: 0.9906 - val_loss: 0.0821 - val_acc: 0.9759\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f9243743ac8>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(X_valid, y_valid))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
